/*
 *    Copyright (c) 2018-2025, boss All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the boss developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: boss
 */

package com.hopes.bizz.close.basic.controller;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hopes.bizz.close.basic.dto.ClosedPersonCompanyAuditDTO;
import com.hopes.bizz.close.basic.dto.ClosedPersonCompanyBindAccountDTO;
import com.hopes.bizz.close.basic.dto.ClosedPersonCompanyDTO;
import com.hopes.bizz.close.basic.dto.ClosedPersonCompanyExcelDTO;
import com.hopes.bizz.close.basic.entity.ClosedPersonCompanyEntity;
import com.hopes.bizz.close.basic.service.ClosedPersonCompanyService;
import com.hopes.bizz.close.basic.vo.ClosedPersonCompanyExcelVO;
import com.hopes.bizz.close.common.constant.VisitorDicConstant;
import com.hopes.boss.common.core.util.R;
import com.hopes.boss.common.excel.annotation.RequestExcel;
import com.hopes.boss.common.excel.annotation.ResponseExcel;
import com.hopes.boss.common.log.annotation.SysLog;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpHeaders;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;

/**
 * 企业人员信息
 *
 * @author pig
 * @date 2024-02-20 13:41:07
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/companyperson" )
@Tag(description = "companyperson" , name = "企业人员信息管理" )
@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
public class ClosedPersonCompanyController {

    private final ClosedPersonCompanyService closedPersonCompanyService;

    /**
     * 分页查询
     * @param page 分页对象
     * @param closedPersonCompany 企业人员信息
     * @return
     */
    @Operation(summary = "分页查询" , description = "分页查询" )
    @GetMapping("/page" )
    @PreAuthorize("@pms.hasPermission('close_company_person_view')" )
    public R getClosedPsersonCompanyPage(Page page, ClosedPersonCompanyDTO closedPersonCompany) {
        LambdaQueryWrapper<ClosedPersonCompanyEntity> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(ObjUtil.isNotNull(closedPersonCompany.getOrgId()), ClosedPersonCompanyEntity::getOrgId, closedPersonCompany.getOrgId());
        wrapper.eq(ObjUtil.isNotNull(closedPersonCompany.getAuditStatus()), ClosedPersonCompanyEntity::getAuditStatus, closedPersonCompany.getAuditStatus());
        wrapper.eq(StrUtil.isNotBlank(closedPersonCompany.getEmployeeType()), ClosedPersonCompanyEntity::getEmployeeType, closedPersonCompany.getEmployeeType());
        wrapper.like(StrUtil.isNotBlank(closedPersonCompany.getKeyword()), ClosedPersonCompanyEntity::getName, closedPersonCompany.getKeyword())
                .or().like(StrUtil.isNotBlank(closedPersonCompany.getKeyword()), ClosedPersonCompanyEntity::getCardNo, closedPersonCompany.getKeyword())
                .or().like(StrUtil.isNotBlank(closedPersonCompany.getKeyword()), ClosedPersonCompanyEntity::getCompanySocialCode, closedPersonCompany.getKeyword());
        wrapper.orderByAsc(ClosedPersonCompanyEntity::getOrgId);
        return R.ok(closedPersonCompanyService.page(page, wrapper));
    }


    /**
     * 分页查询 （移动端）我的申请
     * @param page 分页对象
     * @param closedPersonCompanyDTO 企业人员申请
     * @return
     */
    @Operation(summary = "分页查询" , description = "分页查询" )
    @GetMapping("/selfPage" )
    @PreAuthorize("@pms.hasPermission('close_company_person_view')" )
    public R getAppCompanyPersonSelfPage(Page page, ClosedPersonCompanyDTO closedPersonCompanyDTO) {
        return R.ok(closedPersonCompanyService.queryAppCompanyPersonSelfPage(page, closedPersonCompanyDTO));
    }

    /**
     * 分页查询 （移动端）企业待审批的申请
     * @param page 分页对象
     * @param closedPersonCompanyDTO 企业人员申请
     * @return
     */
    @Operation(summary = "分页查询" , description = "分页查询" )
    @GetMapping("/audit/toDoPage" )
    @PreAuthorize("@pms.hasPermission('visitor_apply_view')" )
    public R getAppCompanyPersonAuditToDoPage(Page page, ClosedPersonCompanyDTO closedPersonCompanyDTO) {
        final List<Integer> auditStatusList = Collections.singletonList(VisitorDicConstant.CLOSE_VISITOR_AUDIT_STATUS_NOT_AUDIT);
        return R.ok(closedPersonCompanyService.queryAppCompanyPersonAuditPage(page, auditStatusList, closedPersonCompanyDTO));
    }


    /**
     * 分页查询 （移动端）企业审批完成的申请
     * @param page 分页对象
     * @param closedPersonCompanyDTO 企业人员申请
     * @return
     */
    @Operation(summary = "分页查询" , description = "分页查询" )
    @GetMapping("/audit/donePage" )
    @PreAuthorize("@pms.hasPermission('visitor_apply_view')" )
    public R getAppVisitorAuditDonePage(Page page, ClosedPersonCompanyDTO closedPersonCompanyDTO) {
        final List<Integer> auditStatusList = Arrays.asList(
                VisitorDicConstant.CLOSE_VISITOR_AUDIT_STATUS_PASS_AUDIT,
                VisitorDicConstant.CLOSE_VISITOR_AUDIT_STATUS_NOT_PASS_AUDIT);
        return R.ok(closedPersonCompanyService.queryAppCompanyPersonAuditPage(page, auditStatusList, closedPersonCompanyDTO));
    }

    /**
     * 通过id查询企业人员信息
     * @param id id
     * @return R
     */
    @Operation(summary = "通过id查询" , description = "通过id查询" )
    @GetMapping("/{id}" )
    @PreAuthorize("@pms.hasPermission('close_company_person_view')" )
    public R getById(@PathVariable("id" ) Long id) {
        return R.ok(closedPersonCompanyService.getById(id));
    }

    /**
     * 新增企业人员信息
     * @param closedPersonCompany 企业人员信息
     * @return R
     */
    @Operation(summary = "新增企业人员信息" , description = "新增企业人员信息" )
    @SysLog("新增企业人员信息" )
    @PostMapping
    @PreAuthorize("@pms.hasPermission('close_company_person_add')" )
    public R save(@RequestBody ClosedPersonCompanyEntity closedPersonCompany) {
        return R.ok(closedPersonCompanyService.save(closedPersonCompany));
    }


    /**
     * 新增并提交企业人员信息
     * @param closedPersonCompany 新增并提交企业人员信息
     * @return R
     */
    @Operation(summary = "新增并提交企业人员信息" , description = "新增并提交企业人员信息" )
    @SysLog("新增并提交企业人员信息" )
    @PostMapping("/saveAndSubmit")
    @PreAuthorize("@pms.hasPermission('close_company_person_add')" )
    public R saveAndSubmit(@RequestBody ClosedPersonCompanyEntity closedPersonCompany) {
        return R.ok(closedPersonCompanyService.saveAndSubmit(closedPersonCompany));
    }

    /**
     * 修改企业人员信息
     * @param closedPersonCompany 企业人员信息
     * @return R
     */
    @Operation(summary = "修改企业人员信息" , description = "修改企业人员信息" )
    @SysLog("修改企业人员信息" )
    @PutMapping
    @PreAuthorize("@pms.hasPermission('close_company_person_edit')" )
    public R updateById(@RequestBody ClosedPersonCompanyEntity closedPersonCompany) {
        return R.ok(closedPersonCompanyService.updateById(closedPersonCompany));
    }

    /**
     * 提交企业人员信息
     *
     * @param id id
     * @return R
     */
    @Operation(summary = "提交企业人员信息", description = "提交企业人员信息")
    @SysLog("提交企业人员信息")
    @GetMapping("/submit/{id}")
    @PreAuthorize("@pms.hasPermission('close_company_person_edit')")
    public R submitById(@PathVariable("id") Long id) {
        return R.ok(closedPersonCompanyService.submitById(id));
    }

    /**
     * 审核企业人员信息
     * @param closedPersonCompanyAuditDTO 审核企业人员信息
     * @return R
     */
    @Operation(summary = "审核企业人员信息" , description = "审核企业人员信息" )
    @SysLog("审核企业人员信息" )
    @PutMapping("/audit")
    @PreAuthorize("@pms.hasPermission('close_company_person_audit')" )
    public R auditById(@RequestBody ClosedPersonCompanyAuditDTO closedPersonCompanyAuditDTO) {
        return R.ok(closedPersonCompanyService.auditById(closedPersonCompanyAuditDTO));
    }

    /**
     * 企业人员绑定账号
     * @param closedPersonCompanyBindAccountDTO 企业人员绑定账号
     * @return R
     */
    @Operation(summary = "企业人员绑定账号" , description = "企业人员绑定账号" )
    @SysLog("企业人员绑定账号" )
    @PostMapping("/bindAccount")
    @PreAuthorize("@pms.hasPermission('close_company_person_audit')" )
    public R bindAccount(@RequestBody ClosedPersonCompanyBindAccountDTO closedPersonCompanyBindAccountDTO) {
        return R.ok(closedPersonCompanyService.bindAccount(closedPersonCompanyBindAccountDTO));
    }

    /**
     * 移除企业账号绑定
     *
     * @param id id
     * @return R
     */
    @Operation(summary = "移除企业账号绑定", description = "移除企业账号绑定")
    @SysLog("移除企业账号绑定")
    @GetMapping("/removeBindAccount/{id}")
    @PreAuthorize("@pms.hasPermission('close_company_person_edit')")
    public R removeBindAccount(@PathVariable("id") Long id) {
        return R.ok(closedPersonCompanyService.removeBindAccount(id));
    }

    /**
     * 通过id删除企业人员信息
     * @param ids id列表
     * @return R
     */
    @Operation(summary = "通过id删除企业人员信息" , description = "通过id删除企业人员信息" )
    @SysLog("通过id删除企业人员信息" )
    @DeleteMapping
    @PreAuthorize("@pms.hasPermission('close_company_person_del')" )
    public R removeById(@RequestBody Long[] ids) {
        return R.ok(closedPersonCompanyService.removeBatchByIds(CollUtil.toList(ids)));
    }

    /**
     * excel模板
     *
     * @return excel 文件流
     */
    @ResponseExcel
    @GetMapping("/template")
    @PreAuthorize("@pms.hasPermission('close_company_person_export')")
    public List<ClosedPersonCompanyExcelVO> template() {
        return closedPersonCompanyService.exportExcel(Collections.singletonList(new ClosedPersonCompanyEntity()));
    }

    /**
     * 导出excel 表格
     * @param closedPersonCompany 查询条件
     * @return excel 文件流
     */
    @ResponseExcel
    @GetMapping("/export")
    @PreAuthorize("@pms.hasPermission('close_company_person_export')")
    public List<ClosedPersonCompanyExcelVO> export(ClosedPersonCompanyDTO closedPersonCompany) {
        LambdaQueryWrapper<ClosedPersonCompanyEntity> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(ObjUtil.isNotNull(closedPersonCompany.getOrgId()), ClosedPersonCompanyEntity::getOrgId, closedPersonCompany.getOrgId());
        wrapper.eq(ObjUtil.isNotNull(closedPersonCompany.getAuditStatus()), ClosedPersonCompanyEntity::getAuditStatus, closedPersonCompany.getAuditStatus());
        wrapper.eq(StrUtil.isNotBlank(closedPersonCompany.getEmployeeType()), ClosedPersonCompanyEntity::getEmployeeType, closedPersonCompany.getEmployeeType());
        wrapper.like(StrUtil.isNotBlank(closedPersonCompany.getKeyword()), ClosedPersonCompanyEntity::getName, closedPersonCompany.getKeyword())
                .or().like(StrUtil.isNotBlank(closedPersonCompany.getKeyword()), ClosedPersonCompanyEntity::getCardNo, closedPersonCompany.getKeyword())
                .or().like(StrUtil.isNotBlank(closedPersonCompany.getKeyword()), ClosedPersonCompanyEntity::getCompanySocialCode, closedPersonCompany.getKeyword());
        wrapper.orderByAsc(ClosedPersonCompanyEntity::getOrgId);
        return closedPersonCompanyService.exportExcel(closedPersonCompanyService.list(wrapper));
    }

    /**
     * 导入excel 表格
     * @param closedPersonCompanyExcelDtoList 查询条件
     * @param bindingResult 错误信息列表
     * @return ok fail
     */
    @PostMapping("/import")
    @PreAuthorize("@pms.hasPermission('close_company_person_export')")
    public R importExcel(@RequestExcel(ignoreEmptyRow = true) List<ClosedPersonCompanyExcelDTO> closedPersonCompanyExcelDtoList, BindingResult bindingResult) {
        return closedPersonCompanyService.importExcel(closedPersonCompanyExcelDtoList, bindingResult);
    }
}
